package utils

import (
	"context"
	"database/sql"
	"fmt"
	_ "github.com/denisenkom/go-mssqldb"
	"log"
)

var Db *sql.DB

//#DataSourceSettings#
//#LocalDataSource: @localhost
//#BEGIN#
//<data-source source="LOCAL" name="@localhost" uuid="b388ef9c-6fdd-45c5-a922-d9eb6f3cc4d3"><database-info product="Microsoft SQL Server" version="15.00.2080" jdbc-version="4.2" driver-name="Microsoft JDBC Driver 9.4 for SQL Server" driver-version="9.4.0.0" dbms="MSSQL" exact-version="15.0.2080" exact-driver-version="9.4"><extra-name-characters>$#@</extra-name-characters><identifier-quote-string>&quot;</identifier-quote-string></database-info><case-sensitivity plain-identifiers="mixed" quoted-identifiers="mixed"/><driver-ref>sqlserver.ms</driver-ref><synchronize>true</synchronize><jdbc-driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</jdbc-driver><jdbc-url>jdbc:sqlserver://localhost</jdbc-url><secret-storage>session</secret-storage><user-name>sa</user-name><schema-mapping><introspection-scope><node kind="database" negative="1"><node kind="schema" negative="1"/></node></introspection-scope></schema-mapping><working-dir>$ProjectFileDir$</working-dir></data-source>
//#END#
//URL	jdbc:sqlserver://;serverName=localhost;databaseName=master
const (
	server   = "localhost"
	port     = 1433
	user     = "sa"
	password = "197912abc"
	database = "GoDb"
)

func init() {
	connStr := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;", server, user, password, port,
		database)
	Db, err := sql.Open("sqlserver", connStr)
	if err != nil {
		log.Fatalln(err.Error())
	}
	ctx := context.Background() //创建一个context,返回一个非nil的空Context.其不会被取消,没有值,没有截止时间
	err = Db.PingContext(ctx)   //验证上面创建的context是否有效,有效则建立一个连接.
	if err != nil {
		log.Fatalln(err.Error())
	}

	fmt.Println("Connected!")
}
